<?php
declare (strict_types=1);

namespace app\exam\logic\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\common\logic\category\Category;
use app\exam\model\admin\ModulePoint as ModulePointModel;
use app\exam\validate\admin\ModulePoint as ModulePointValidate;
use think\Model;

/**
 * Class ModulePoint
 *
 * @package app\exam\logic\admin
 */
class ModulePoint extends ModulePointModel
{
    /**
     * 获取树形结构列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-15
     *
     * @param int         $moduleId [description]
     * @param int|integer $pid      [description]
     * @return [type]                [description]
     */
    public function getTreeList(int $moduleId)
    {
        $data = $this->where('module_id', $moduleId)->select()->each(function($i){
            $i->cates = app(Category::class)->parseTitle($i->category, 3);
        });

        return $data;
    }

    /**
     * 根据ID获取知识点信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-15
     * @param  int    $id [description]
     * @return [type]     [description]
     */
    public function getPointById(int $id): Model
    {
        return $this->findOrFail($id);
    }

    /**
     * 新建知识点
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-15
     *
     * @param int   $moduleId [description]
     * @param array $data     [description]
     * @return [type]           [description]
     */
    public function createPoint(int $moduleId, array $data)
    {
        $data['module_id'] = $moduleId;
        validate(ModulePointValidate::class)->scene('create')->check($data);

        $save = [
            'module_id'      => $moduleId,
            'title'          => $data['title'],
            // 'point_limit'    => $data['point_limit'] ?? 1,
            // 'question_limit' => $data['question_limit'] ?? 1,
        ];

        if (isset($data['category'])) {
            $save['category'] = $save['fullcategorypath'] = $data['category'];
        }
        $re = $this::create($save);
        if ($re->isEmpty()) {
            throw new LogicException('新建失败，请重试');
        }
        
        Log::write('新建知识点【' . $data['title'] . '/' . $re->id . '】', 'point', 'create', true);
    }

    /**
     * 编辑知识点
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-15
     * @param  int    $pointId [description]
     * @param  int    $moduleId  [description]
     * @param  array  $data      [description]
     * @return [type]            [description]
     */
    public function editPoint(array $data)
    {
        validate(ModulePointValidate::class)->scene('edit')->check($data);
        $point = $this->getPointById($data['id']);

        $point->title            = $data['title'];
        // $point->point_limit      = $data['point_limit'];
        // $point->question_limit   = $data['question_limit'];
        $point->category         = $data['category'];
        $point->fullcategorypath = $data['category'];

        if (!$point->save()) {
            throw new LogicException('修改知识点失败');
        }
        
        Log::write('编辑知识点【' . $data['title'] . '/' . $data['id'] . '】', 'point', 'update', true);
    }

    /**
     * 删除知识点
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-15
     * @param  int    $pointId [description]
     * @return [type]            [description]
     */
    public function deletePoint(int $pointId)
    {
        $point = $this->getPointById($pointId);

        if (!$point->delete()) {
            throw new LogicException('删除失败');
        }
        
        Log::write('删除知识点【' . $point['title'] . '/' . $pointId . '】', 'point', 'delete', true);
    }
}